import 'package:flutter/material.dart';
import 'data_source/data.dart';

import '../local/pages/context_header.dart';
import 'ui_demo.dart';

class AllDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new NavDemo();
  }
}

class DemoNavigation extends StatefulWidget {
  final String title;
  DemoNavigation({key, this.title});
  @override
  State<StatefulWidget> createState() => new _DemoNavigationState();
}

class _DemoNavigationState extends State<DemoNavigation> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("flutter 示例"),
          centerTitle: true,
        ),
        body: ListView.builder(
          itemBuilder: _navigatorListItem,
          itemCount: demos.length,
        ));
  }

  Widget _navigatorListItem(BuildContext context, int index) {
    return new GestureDetector(
        onTap: () {
          App.navigateTo(context, "/demo${(index + 1).toString()}",
              transition: TransitionType.inFromLeft);
        },
        child: Container(
            //alignment: Alignment.center,
            padding: EdgeInsets.only(left: 10, top: 10, bottom: 0),
            margin: EdgeInsets.all(10),
            child: Text(
              demos[index].title,
              style: TextStyle(
                fontSize: 18,
              ),
            )));
  }
}

class NavDemo extends StatelessWidget {
  List<NavItem> _list = [
    NavItem("小部件", Icons.radio_button_checked, "/demo/display",
        "1,4,9,10,11,22"),
    NavItem("视图", Icons.list, '/demo/display', "2,3,5,6,7,12,13,14"),
    NavItem(
        "状态管理", Icons.add_alert, '/demo/display', "8,15,16,17,18,19,20,21"),
    NavItem("本机环境", Icons.add_a_photo, '/demo/display', "23,24,25,26,27,28,29,30"),
    NavItem("ALL", Icons.border_all, '/demo/all', ""),
  ];
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text("flutter 示例"),
          leading: IconButton(icon: Icon(Icons.home), onPressed: (){
            //App.navigateTo(context, "/");
          }),
          centerTitle: true,
        ),
        body: GridView(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
          ),
          children: _list,
        ));
  }
}

class NavItem extends StatelessWidget {
  final String title;
  final IconData iconData;
  final String path;
  final String subItem;
  NavItem(this.title, this.iconData, this.path, this.subItem);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return InkWell(
      onTap: () {
        App.navigateTo(context, "$path?param=${this.subItem}");
      },
      child: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[Icon(iconData,size: 40.0,), Text(title)],
        ),
      ),
    );
  }
}

class DemoDisplay extends StatelessWidget {
  final List<Demo> _list;
  DemoDisplay(this._list);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("示例"),
      ),
      body: GridView(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
        ),
        children: _list.map((value) {
          return InkWell(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[Icon(Icons.star,size: 40.0,), Text(value.title)],
            ),
            onTap: (){
              int index=demos.indexOf(value);
              App.navigateTo(context, "/demo${(index+1).toString()}");
            },
          );
        }).toList(),
      ),
    );
  }
}
